package com.zrrd.mcl.order.receiver;

import com.rabbitmq.client.Channel;
import com.zrrd.mcl.model.enums.OrderStatus;
import com.zrrd.mcl.model.enums.ProcessStatus;
import com.zrrd.mcl.model.order.OrderInfo;
import com.zrrd.mcl.order.service.OrderService;
import com.zrrd.mcl.rabbit.constant.MqConstant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderReceiver {

    @Autowired
    private OrderService orderService;

    //接收关闭订单消息
    @RabbitListener(queues = MqConstant.QUEUE_ORDER_CANCEL)
    public void closeOrder(Long orderId, Message message, Channel channel) throws IOException {
        System.out.println("收到了关闭订单的消息=====>"+orderId);
        if(orderId != null){
            //根据订单id查询出订单信息
            OrderInfo orderInfo = orderService.getById(orderId);
            //如果当前订单状态是未支付
            if(orderInfo.getOrderStatus().equals("UNPAID")){
                //将当前订单状态改为关闭
                orderInfo.setOrderStatus(OrderStatus.CLOSED.name());
                orderInfo.setProcessStatus(ProcessStatus.CLOSED.name());
                //执行修改
                orderService.updateById(orderInfo);
            }
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
